package gameoflife.main;
import gameoflife.CellRenderer;
import gameoflife.Grid;
import gameoflife.clips.ClipsEvalProcess;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

import CLIPSJNI.Environment;

public class Game {
	
	public static final int SIZE = 30;
	
	Environment clips;
	JTable grid;

	private void setUpClipsEngine() {
		clips = new Environment();
		clips.load("life.clp");
		clips.reset();
	}
	
	public void addComponentsToPane(Container pane) {
		pane.setLayout(new BorderLayout());
		pane.setPreferredSize(pane.getSize());
		
		grid = new JTable(new Grid());
		grid.setDefaultRenderer(Boolean.class, new CellRenderer());
		grid.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		for (int i = 0; i < grid.getColumnModel().getColumnCount(); i++) {
			grid.getColumnModel().getColumn(i).setPreferredWidth(5);
		}

		pane.add(grid, BorderLayout.CENTER);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
		buttonPanel.add(Box.createRigidArea(new Dimension(0,5)));

		JButton button = new JButton();
		button.setText("Iterate");
		button.setAlignmentX(Component.LEFT_ALIGNMENT);
		button.setMaximumSize(null);
		button.setPreferredSize(null);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				iterateButtonAction(e);
				
			}
		});

		buttonPanel.add(button);
		buttonPanel.add(Box.createRigidArea(new Dimension(0,5)));
		
		button = new JButton();
		button.setText("Start");
		button.setAlignmentX(Component.LEFT_ALIGNMENT);
		button.setMaximumSize(null);
		button.setPreferredSize(null);
		buttonPanel.add(button);
		buttonPanel.add(Box.createRigidArea(new Dimension(0,5)));
		
		button = new JButton();
		button.setText("Exit");
		button.setAlignmentX(Component.LEFT_ALIGNMENT);
		button.setMaximumSize(null);
		button.setPreferredSize(null);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
			
		});
		buttonPanel.add(button);
		pane.add(buttonPanel, BorderLayout.LINE_END);
	}
	
	private void createAndShowGUI() {
        JFrame frame = new JFrame("Game of Life");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 510);
        setUpClipsEngine();
        addComponentsToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);

	}
	
	private void iterateButtonAction(ActionEvent e) {
		ClipsEvalProcess eval = new ClipsEvalProcess(grid.getModel(), clips);
		eval.addPropertyChangeListener(
			     new PropertyChangeListener() {
			         public  void propertyChange(PropertyChangeEvent evt) {
			             if ("state".equals(evt.getPropertyName())) {
			                 grid.repaint();
			             }
			         }
			     });
		eval.execute();

	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Game().createAndShowGUI();
            }
        });
	}

}
